const { get } = require("jquery");

define(['jquery', 'bootstrap', 'backend', 'table', 'form','vue'], function ($, undefined, Backend, Table, Form,Vue) {

    var Controller = {
        index: function () {
            const vm =new Vue({
                el: '#app',
                data:{               
                    user:null,//用户信息
                    head:[], //头部导航
                    banner:[],//轮播图
                    searchQuery: '',//搜索框
                    suggestions: [],//搜索建议
                    showSuggestions: false,//是否显示建议列表
                    selectedIndex: -1,//当前选中的建议索引
                    page:1,
                    list:[],
                    navlist : [],
                    navItem:1,
                    resouceItem:[],
                    forms:[],
                    more:true,
                    
                },
                created() {
                    that = this;
                    Fast.api.ajax({
                        url: "ajax/auth",
                        loading: false,
                        async:true,
                        success:function(result) {
                            if (result.code == 200) {
                                that.user = result.data;
                            }
                        }
                    });
                    Fast.api.ajax({
                        url: "ajax/getcate",
                        loading: false,
                        async:true,
                        success:function(result) {

                            if (result.code == 200) {
                                that.banner = result.data.banner;
                                that.head = result.data.head;
                            }
                        }
                    });
                    Fast.api.ajax({
                        url: "ajax/resource",
                        loading: false,
                        async:true,
                        success:function(result) {

                            if (result.code == 200) {
                                that.navlist  = result.data;
                            }
                        }
                    });
                    this.handleShow(true);
                },
                mounted() {
                    if (this.$el.contains(this.$refs.loadNode)) {
                        this.$el.removeChild(this.$refs.loadNode);
                    }
                    // 点击页面其他地方时关闭建议列表
                    document.addEventListener('click', (e) => {
                        if (!e.target.closest('.search-box')) {
                            that.showSuggestions = false;
                        }
                    });
                    window.addEventListener('scroll', this.handleScroll);
                   
                },
                beforeDestroy() {
                    // 页面销毁前移除滚动事件监听
                    window.removeEventListener('scroll', this.handleScroll);
                  },
                methods:{
                    handleScroll() {
                        const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
                        const windowHeight = window.innerHeight || document.documentElement.clientHeight;
                        const documentHeight = document.documentElement.scrollHeight;
                        if (scrollTop + windowHeight >= (documentHeight-30)) {
                            that.getData();
                        }
                    },
                    showFavorites(id){
                        if( !this.user){
                            window.location.href = '/login';
                            return;
                        }
                        if( this.user.level == 0){
                            Fast.api.layer.confirm('需要成为闻道会员可访问', {title:'提示',icon: 3}, function(index){
                                Fast.api.close(index);
                                that.handleBuy();
                            });
                            return;
                        }
                        Fast.api.ajax({
                            url: "member/submit",
                            loading: false,
                            async:true,
                            data:{t:'wr',id:id,c:'fa'},
                           
                        });
                    },
                    handleBuy(){
                        Fast.api.open('/member/level','会员购买',{
                            minmax: false,
                            shadeClose:false,
                            shade:0.3,
                            callback: function (data) {
                                if(data.code == 1){
                                    Fast.api.close();
                                    Fast.api.success(data.msg);
                                }else{
                                    Fast.api.error(data.msg);
                                }
                            }
                        });
                    },
                    handleTel(item){
                        
                        if( !this.user){
                            window.location.href = '/login';
                            return;
                        }
                        if( this.user.level == 0){
                            Fast.api.layer.confirm('需要成为闻道会员可访问', {title:'提示',icon: 3}, function(index){
                                Fast.api.close(index);
                                that.handleBuy();
                            });
                            return;
                        }
                        Fast.api.open('/member/submit?c=tel&t=wr&id='+item.id,'联系询价',{
                            minmax: false,
                            shadeClose:false,
                            shade:0.3,
                            callback: function (data) {
                                if(data.code == 1){
                                    Fast.api.close();
                                    Fast.api.layer.confirm('手机号:'+item.mobile, {title:'联系方式'});
                                }else{
                                    Fast.api.error(data.msg);
                                }
                            }
                        });
                    },
                    handle(url,isLogin=false){
                        let that = this;
                        if(isLogin && !this.user){
                            window.location.href = '/login';
                            return;
                        }
                        if(isLogin && this.user.level == 0){
                            Fast.api.layer.confirm('需要成为闻道会员可访问', {title:'提示',icon: 3}, function(index){
                                Fast.api.close(index);
                                that.handleBuy();
                            });
                            return;
                        }
                        window.location.href = url;
                    },
                    handleCate(item){
                        this.navItem = item.id;
                        this.handleShow(true);
                    },
                    
                    handItem(item, index) {
                        console.log(item, index);
                        this.forms[index].value = item;
                        this.handleShow();
                    },

                    handleShow(nav=false) {
                        this.page = 1;
                        this.more = true;
                        if(nav){
                            let that = this;
                            this.forms = [];
                            Fast.api.ajax({
                                url: "ajax/resourcetype",
                                data: {
                                    'type_id':this.navItem,
                                },
                                async:true,
                                loading: false,
                                success:function(result) {
                                    if (result.code == 200) {
                                        that.resouceItem = result.data;
                                        result.data.forEach((element,index) => {
                                            that.forms.push({value:'全部'})
                                        });
                                    }
                                }
                            });
                        }
                        this.getData();                       
                        
                    },
                    getData(){
                        let that = this;
                        if(that.more == false){
                            return;
                        }
                        if(that.page > 1){
                            if(!this.user){
                                window.location.href = '/login';
                                return;
                            }
                            if( this.user.level == 0){
                                Fast.api.layer.confirm('需要成为闻道会员可访问', {title:'提示',icon: 3}, function(index){
                                    Fast.api.close(index);
                                    that.handleBuy();
                                });
                                return ;
                            }
                        }
                        Fast.api.ajax({
                            url: 'resource/index',
                            data: {
                                keywords:this.searchQuery,
                            item:that.forms||[],
                            type_id:this.navItem,
                            page:this.page
                            },
                            async:true,
                            loading: false,
                            success:function(result) {
                                if (result.code == 200) {
                                    if(that.page == 1){
                                     that.list = result.data;
                                    }else{
                                      that.list.push(...result.data);
                                    }
                                    if(result.data.length <10){
                                     that.more = false;
                                    }
                                    page = that.page;
                                    that.page = page+1;
                                 }
                            }
                        });
                    },
                    handleSearch() {
                        if (!this.searchQuery) {
                            this.suggestions = [];
                            this.showSuggestions = false;
                            return;
                        } 
                        Fast.api.ajax({
                            url: 'ajax/searchlog',
                            data: {
                                query: this.searchQuery,
                            },
                            async:true,
                            loading: false,
                            success:function(result) {
                                if (result.code == 200) {
                                    that.suggestions = result.data;
                                    that.showSuggestions = true;
                                    that.selectedIndex = -1;
                                }
                            }
                        });
                        
                    },
                    handleKeyDown(e) {
                        if (!this.suggestions.length) return;
                    
                        switch(e.key) {
                            case 'ArrowDown':
                                e.preventDefault();
                                this.selectedIndex = (this.selectedIndex + 1) % this.suggestions.length;
                                break;
                            case 'ArrowUp':
                                e.preventDefault();
                                this.selectedIndex = this.selectedIndex <= 0 ? this.suggestions.length - 1 : this.selectedIndex - 1;
                                break;
                            case 'Enter':
                                if (this.selectedIndex > -1) {
                                    this.selectSuggestion(this.suggestions[this.selectedIndex]);
                                } else {
                                    this.submitSearch();
                                }
                                break;
                            case 'Escape':
                                this.showSuggestions = false;
                                break;
                        }
                    },
                    selectSuggestion(item) {
                        this.searchQuery = item.name;
                        this.showSuggestions = false;
                        
                    },
                    submitSearch() {
                       this.handleShow();
                    }
                }
            });
        },
        add: function () {
            Form.api.bindevent($("form[role=form]"));
        },
        detail:function(){
            const vm =new Vue({
                el: '#app',
                data:{               
                    user:null,//用户信息
                    head:[], //头部导航
                    item:{followlist:[ ],commonslist:[]},
                },
                created() {
                    that = this;
                    Fast.api.ajax({
                        url: "ajax/auth",
                        loading: false,
                        async:true,
                        success:function(result) {
                            if (result.code == 200) {
                                that.user = result.data;
                            }
                        }
                    });
                    Fast.api.ajax({
                        url: "ajax/getcate",
                        loading: false,
                        async:true,
                        success:function(result) {

                            if (result.code == 200) {
                                
                                that.head = result.data.head;
                            }
                        }
                    });
                    this.getData();
                },
                mounted() {
                    if (this.$el.contains(this.$refs.loadNode)) {
                        this.$el.removeChild(this.$refs.loadNode);
                    }
                },
                methods:{
                    handle(url,isLogin){
                        let that = this;
                        if(isLogin && !this.user){
                            window.location.href = '/login';
                            return;
                        }
                        if(isLogin && this.user.level == 0){
                            Fast.api.layer.confirm('需要成为闻道会员可访问', {title:'提示',icon: 3}, function(index){
                                Fast.api.close(index);
                                that.handleBuy();
                            });
                            return;
                        }
                        window.location.href = url;
                    },
                    showFavorites(id){
                        if( !this.user){
                            window.location.href = '/login';
                            return;
                        }
                        if( this.user.level == 0){
                            Fast.api.layer.confirm('需要成为闻道会员可访问', {title:'提示',icon: 3}, function(index){
                                Fast.api.close(index);
                                that.handleBuy();
                            });
                            return;
                        }
                        Fast.api.ajax({
                            url: "member/submit",
                            loading: false,
                            async:true,
                            data:{t:'wr',id:id,c:'fa'},
                         
                        });
                    },
                    showComment(id){
                        if( !this.user){
                            window.location.href = '/login';
                            return;
                        }
                        if( this.user.level == 0){
                            Fast.api.layer.confirm('需要成为闻道会员可访问', {title:'提示',icon: 3}, function(index){
                                Fast.api.close(index);
                                that.handleBuy();
                            });
                            return;
                        }
                        Fast.api.open('/member/submit?c=comment&t=wr&id='+id,'我要评论',{
                            minmax: false,
                            shadeClose:false,
                            shade:0.3,
                            callback: function (data) {
                                if(data.code == 1){
                                    Fast.api.close();
                                    Fast.api.success(data.msg);
                                }else{
                                    Fast.api.error(data.msg);
                                }
                            }
                        });
                    },
                    handleTel(item){
                        
                        if( !this.user){
                            window.location.href = '/login';
                            return;
                        }
                        if( this.user.level == 0){
                            Fast.api.layer.confirm('需要成为闻道会员可访问', {title:'提示',icon: 3}, function(index){
                                Fast.api.close(index);
                                that.handleBuy();
                            });
                            return;
                        }
                        Fast.api.open('/member/submit?c=tel&t=wr&id='+item.id,'联系询价',{
                            minmax: false,
                            shadeClose:false,
                            shade:0.3,
                            callback: function (data) {
                                if(data.code == 1){
                                    Fast.api.close();
                                    Fast.api.layer.confirm('手机号:'+item.mobile, {title:'联系方式'});
                                }else{
                                    Fast.api.error(data.msg);
                                }
                            }
                        });
                    },
                    handleBuy(){
                        Fast.api.open('/member/level','会员购买',{
                            minmax: false,
                            shadeClose:false,
                            shade:0.3,
                            callback: function (data) {
                                if(data.code == 1){
                                    Fast.api.close();
                                    Fast.api.success(data.msg);
                                }else{
                                    Fast.api.error(data.msg);
                                }
                            }
                        });
                    },
                    handleTel(item){
                        if( !this.user){
                            window.location.href = '/login';
                            return;
                        }
                        if( this.user.level == 0){
                            Fast.api.layer.confirm('需要成为闻道会员可访问', {title:'提示',icon: 3}, function(index){
                                Fast.api.close(index);
                                that.handleBuy();
                            });
                            return;
                        }
                        Fast.api.open('/member/submit?c=tel&t=wr&id='+id,'联系询价',{
                            minmax: false,
                            shadeClose:false,
                            shade:0.3,
                            callback: function (data) {
                                if(data.code == 1){
                                    Fast.api.close();
                                    Fast.api.layer.confirm('手机号:'+item.mobile, {title:'联系方式'});
                                }else{
                                    Fast.api.error(data.msg);
                                }
                            }
                        });
                    },
                    getData(){
                        let that = this;
                        Fast.api.ajax({
                            url:'resource/detail',
                            data: {
                                id:aid
                            },
                            async:true,
                            loading: false,
                            success:function(result) {
                                if (result.code == 200) {
                                    that.item = result.data;
                                 }
                            }
                        });
                    }
                }
            });
        }
    };
    return Controller;
});
